package com.yaoli.ajob;

/**
 * Created by will on 2017/4/27.
 */
public class _240_Searcha2DMatrixII {
    public static void main(String[] args) {

        _240_Searcha2DMatrixII searcha2DMatrix = new _240_Searcha2DMatrixII();

        int matrix[][] = {{1,4,7,11,15},
                {2,5,8,12,19},
                {3,6,9,16,22},
                {10,13,14,17,24},
                {18,21,23,26,30}};

        searcha2DMatrix.searchMatrix(matrix,3);

    }

    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix.length == 0 || matrix[0].length == 0){
            return false;
        }
        boolean flag = false;

        int row = 0;
        int col = matrix[0].length - 1;

        while(col >= 0 || row <= matrix.length - 1){
            int temp = matrix[row][col];

            if(temp == target){
                return true;
            }
            //if(temp < target){
            if(target < temp){
                col--;
            }else{
                row ++;
            }
        }

        return flag;
    }
}
